package com.no1.zy.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

@Configuration
//指定文件属性位置
//@PropertySource("classpath:application.properties")
//将JdbcProperties对象注入到配置类JdbcConfig中
//@EnableConfigurationProperties({JdbcProperties.class})
public class JdbcConfig {
    //第一版本：java类配置方式
//    @Value("${jdbc.driverClassName}")
//    String driverClassName;
//    @Value("${jdbc.url}")
//    String url;
//    @Value("${jdbc.username}")
//    String username;
//    @Value("${jdbc.password}")
//    String password;
//
//    @Bean
//    public DruidDataSource dataSource(){
//        DruidDataSource dataSource=new DruidDataSource();
//        dataSource.setDriverClassName(driverClassName);
//        dataSource.setUrl(url);
//        dataSource.setUsername(username);
//        dataSource.setPassword(password);
//        return dataSource;

    //}

    //第二版本：spring boot 配置方式
    /*@Bean
    @ConfigurationProperties(prefix = "jdbc")
    public DruidDataSource dataSource(JdbcProperties jdbc){
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setDriverClassName(jdbc.getDriverClassName());
       dataSource.setUrl(jdbc.getUrl());
        dataSource.setUsername(jdbc.getUsername());
       dataSource.setPassword(jdbc.getPassword());
       return dataSource;
    }*/

    //第三种：更优雅的注入：当年发现只有一个Bean需要注入时，你可以这样干
   @Bean
   @ConfigurationProperties(prefix = "jdbc")
    public DataSource dataSource(){
       return new DruidDataSource();
   }
}
